import numpy as np  

def otsu_threshold_float(image_data):  
    # 将灰度值从0-1映射到0-255  
    image_scaled = (image_data * 255).astype(np.uint8)  
    
    # 计算图像的直方图  
    hist, bins = np.histogram(image_scaled.flatten(), 256, [0, 257])  
    
    # 计算总的像素点数  
    total_pixels = image_scaled.size  
        
    # 计算累积和  
    sum_b = np.cumsum(hist)  
        
    # 计算累积权重和  
    sum_w = np.cumsum(hist * np.arange(0, 256))  
        
    # 计算前景和背景的类间方差  
    threshold = 0  
    max_var = 0  
        
    # 遍历所有可能的阈值  
    for i in range(1, 256):  
        w_b = sum_b[i - 1]  # 背景像素数  
        w_f = total_pixels - w_b  # 前景像素数  
            
        # 避免除以零  
        if w_b == 0 or w_f == 0:  
            continue  
        
        # 计算背景和前景的均值  
        mean_b = sum_w[i - 1] / w_b  
        mean_f = (sum_w[255] - sum_w[i - 1]) / w_f  
            
        # 计算类间方差  
        var_between = w_b * w_f * (mean_b - mean_f) ** 2  
            
        # 如果当前类间方差大于之前的最大值，则更新阈值  
        if var_between > max_var:  
            max_var = var_between  
            threshold = i  
        
    # 将阈值映射回0-1的范围  
    threshold_scaled = threshold / 255.0  
        
    return threshold_scaled  

# 示例用法  
if __name__ == "__main__":
    image_data = np.array([[0.0, 0.1, 0.9], [0.8, 0.5, 0.9], [0.1, 0.2, 0.1]])  
    threshold = otsu_threshold_float(image_data)  
    print(f"Otsu's threshold: {threshold}")